package org.lx.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lx.common.utils.bean.BeanUtils;
import org.lx.nursing.domain.NursingPlan;
import org.lx.nursing.domain.NursingProjectPlan;
import org.lx.nursing.dto.NursingPlanDto;
import org.lx.nursing.mapper.NursingPlanMapper;
import org.lx.nursing.mapper.NursingProjectPlanMapper;
import org.lx.nursing.service.INursingPlanService;
import org.lx.nursing.vo.NursingPlanVo;
import org.lx.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = getById(id);
        List<NursingProjectPlanVo> projectList = nursingProjectPlanMapper.getProjectListByPlanId(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        nursingPlanVo.setProjectPlans(projectList);
        return nursingPlanVo;
    }

    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        int rows = nursingProjectPlanMapper.insertBatch(projectPlans, nursingPlan.getId());
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return rows > 0 ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
            nursingProjectPlanMapper.insertBatch(projectPlans, nursingPlanDto.getId());
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return updateById(nursingPlan) ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids) {
        nursingProjectPlanMapper.batchDeleteByPlanIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return nursingPlanMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int deleteNursingPlanById(Long id) {
        return nursingPlanMapper.deleteById(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<NursingPlan> getAllNursingPlans() {
        List<NursingPlan> list = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (list != null && !list.isEmpty()) return list;
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingPlan::getStatus, 1);
        list = list(queryWrapper);
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
